package org.book.service.core;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import lombok.extern.slf4j.Slf4j;
import org.book.bean.Borrow;
import org.book.mapper.BookMapper;
import org.book.mapper.BorrowMapper;
import org.book.service.IBorrowService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author dancun
 * @since 2021-03-05
 */
@Service
@Slf4j
public class BorrowServiceImpl extends ServiceImpl<BorrowMapper, Borrow> implements IBorrowService {
    @Autowired
    BorrowMapper borrowMapper;
    @Autowired
    BookMapper bookMapper;

    @Override
    public boolean borrowbook(Borrow borrow) {
        borrowMapper.insert(borrow);
        bookMapper.borrowbook(borrow.getIsbn());

        return true;
    }

    @Override
    public boolean retturnbook(Borrow borrow) throws Exception {
        LambdaUpdateWrapper<Borrow> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.eq(Borrow::getBorrowid, borrow.getBorrowid()).set(Borrow::getReturndate, new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        borrowMapper.update(null,lambdaUpdateWrapper);
        bookMapper.returnbook(borrow.getIsbn());
        return true;
    }

    @Override
    public List<Borrow> listBorrow(QueryWrapper wrapper) {
        List<Borrow> list = null;
        try{
            list=borrowMapper.listBorrow( wrapper);
        }catch (Exception e){
            log.info(e.getStackTrace().toString());
        }
        return list;
    }
}
